package com.renli.unitTest;

import com.renli.mapper.JobPositionMapper;
import com.renli.pojo.JobPosition;
import com.renli.utils.JDBCUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class JobPositionMapperTest {

    @BeforeEach
    public void setUp() {
        // 在每个测试方法之前执行的设置代码，例如初始化数据库连接等
        System.err.println(JDBCUtils.getConnection());
    }

    @Test
    @Order(1)
    public void testAddJobPosition() {
        JobPosition jobPosition = new JobPosition();
        jobPosition.setPositionName("Manager");
        jobPosition.setDepartment("HR");
        jobPosition.setDescription("Manage HR department");

        try {
            boolean result = JobPositionMapper.add(jobPosition);
            assertTrue(result);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(2)
    public void testGetJobPositionById() {
        int positionId = 1; // 假设存在职位ID为1的职位
        try {
            JobPosition jobPosition = JobPositionMapper.getJobPositionById(positionId);
            assertNotNull(jobPosition);
            System.err.println(jobPosition);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(3)
    public void testDeleteJobPosition() {
        int positionId = 2; // 假设要删除职位ID为1的职位
        try {
            boolean result = JobPositionMapper.delete(positionId);
            assertTrue(result);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(4)
    public void testUpdateJobPosition() {
        JobPosition jobPosition = new JobPosition();
        jobPosition.setPositionId(1); // 假设要更新职位ID为2的职位信息
        jobPosition.setPositionName("Updated Position");
        jobPosition.setDepartment("Updated Department");
        jobPosition.setDescription("Updated Description");

        try {
            boolean result = JobPositionMapper.update(jobPosition);
            assertTrue(result);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(5)
    public void testGetTotalJobPositionCount() {
        try {
            int totalJobPositionCount = JobPositionMapper.getTotalJobPositionCount();
            assertTrue(totalJobPositionCount > 0);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(6)
    public void testGetAllJobPositions() {
        try {
            List<JobPosition> jobPositions = JobPositionMapper.getAllJobPositions();
            assertNotNull(jobPositions);
            System.err.println(jobPositions);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(7)
    public void testGetJobPositionsByPage() {
        String positionName = "Updated"; // 假设要搜索的职位名称包含"Updated"
        int pageNum = 1; // 假设要查询第一页
        int pageSize = 10; // 假设每页显示10条记录
        try {
            List<JobPosition> jobPositions = JobPositionMapper.getJobPositionsByPage(positionName, pageNum, pageSize);
            assertNotNull(jobPositions);
            System.err.println(jobPositions);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(8)
    public void testBatchDeleteJobPositions() {
        List<Integer> positionIds = new ArrayList<>();
        positionIds.add(3); // 假设要删除职位ID为3的职位
        positionIds.add(4); // 假设要删除职位ID为4的职位

        try {
            boolean result = JobPositionMapper.batchDelete(positionIds);
            assertTrue(result);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }
}
